/*****************************************************************************/
/*                                                                           */
/*    Domino Operation System Kernel Module                                  */
/*                                                                           */
/*    Copyright (C) 2005 Laszlo Arvai                                        */
/*                                                                           */
/*    ------------------------------------------------------------------     */
/*    drvRealTimeClock - Real Time Clock Driver Module                       */
/*****************************************************************************/

///////////////////////////////////////////////////////////////////////////////
// Includes
#include "krnlMessageQueue.h"
#include "krnlMessages.h"
#include "drvRealTimeClock.h"
#include "krnlRealTimeClock.h"

///////////////////////////////////////////////////////////////////////////////
// Module variables
static krnlDateTime l_date_time;
static dosByte counter = 0;

//////////////////////////////////////////////////////////////////////////////
// Send real time clock message
void krnlSendRealTimeClockMessage(void)
{
  krnlMessage* message;

  // send message
  message = krnlAllocateAsyncMessage();
  if( message != NULL )
  {
    message->Id = msgREAL_TIME_MESSAGE;

    // time
    message->Data[0] = l_date_time.Hour;
    message->Data[1] = l_date_time.Minute;
    message->Data[2] = l_date_time.Second;

    // date
    message->Data[3] = (l_date_time.Year) & 0xff;
    message->Data[4] = (l_date_time.Year)>> 8;
    message->Data[5] = l_date_time.Month;
    message->Data[6] = l_date_time.Day;

    //  day of week
    message->Data[7] = l_date_time.DayOfWeek;
  }
}

///////////////////////////////////////////////////////////////////////////////
// Init real time clock
void drvInitRealTimeClock(void)
{
	l_date_time.Hour = 0;
	l_date_time.Minute = 0;
	l_date_time.Second = 0;

	l_date_time.Year = 2006;
	l_date_time.Month = 6;
	l_date_time.Day = 30;
	l_date_time.DayOfWeek = 5;

  krnlSendRealTimeClockMessage();
}

///////////////////////////////////////////////////////////////////////////////
// Real time clock message handler
void guiRealTimeClockMessageHandler( krnlMessage* in_message )
{
	counter++;

	if( counter == 10 )
	{
		counter = 0;

		krnlAddOneSecond( &l_date_time );

		krnlSendRealTimeClockMessage();
	}
}

///////////////////////////////////////////////////////////////////////////////
// Get Date & Time
void krnlGetDateTime( krnlDateTime* in_datetime )
{
	*in_datetime = l_date_time;
}
